package yang.action;


import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

import yang.pageModel.Json;
import yang.pageModel.Patient;
import yang.pageModel.User;
import yang.service.PatientServiceI;

/**
 * 挂号单请求
 * @ClassName: PatientAction
 * @Description: 所有挂号单的请求都发送到这里
 * @author: Administrator
 * @date: 2014年6月13日 上午8:43:30
 */
@Action("patientAction")
public class PatientAction extends BaseAction implements ModelDriven<Patient> {

	private static final Logger logger = Logger.getLogger(PatientAction.class);
	User user=(User) ActionContext.getContext().getSession().get("user");//获取会话里用户信息
	Patient info =new Patient();
	
	/**
	 * 
	 * @Title: getModel
	 * @Description: 模型驱动需要
	 * @return Patient
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	@Override
	public Patient getModel() {
		return info;
	}

	private PatientServiceI patientService;
	/**
	 * 
	 * @Title: deleteById
	 * @Description: 按ID删除挂号单
	 * @return: void
	 */
	public void deleteById(){
		User user=(User)ActionContext.getContext().getSession().get("user");
		ActionContext.getContext().getSession().get("user");
		try {
			super.writeJson(patientService.deletePatient(info));
			logger.info(user.getUserTruename()+"删除了一个挂号单");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(user.getUserTruename()+"删除挂号单失败");
		}
	}
	/**
	 * 
	 * @Title: findPatient
	 * @Description: 查找挂号单
	 * @return: void
	 */
	public void findPatient(){	
		int o=0;
		
		if (user!=null) {			
				if (user.getUserRole().equals("医生")) {
					o=1;
					info.setDoctorId(user.getUserNo());
				}else if (user.getUserRole().equals("缴费")) {//没有用
					o=2;
				}else if (user.getUserRole().equals("药房")) {//没有用
					o=3;
				}
			}
		info.setPatientType(o);//增加Where条件
		try {
			super.writeJson(patientService.findPatient(info));
			logger.info(user.getUserTruename()+"查询挂号单，已按条件找到");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(user.getUserTruename()+"查询挂号单，出现错误");
		}		
	}

	/**
	 * @Title: savePatient
	 * @Description: 新建一个挂号单
	 * @return: void
	 */
	public void savePatient() {
		Json json=new Json();
		try {
			Patient patient= patientService.savePatients(info);
			logger.info(user.getUserTruename()+"新建了一个挂号单");
			json.setMsg("添加成功");
			json.setSuccess(true);
			json.setObj(patient);
		} catch (Exception e) {
			json.setMsg("出现了未知错误，请再试一遍");	
			e.printStackTrace();
			logger.error(user.getUserTruename()+"创建挂号单时出现错误");
		}
		super.writeJson(json);
	}

	/**
	 * @Title: updatePatient
	 * @Description:多个地方调用这个方法，用来判断当前挂号单显示的位置
	 * @return: void
	 */
	public void updatePatient() {
		Json json=new Json();
		try {
			patientService.updatePatient(info);
			json.setSuccess(true);
			json.setMsg("执行成功");
			logger.info(user.getUserTruename()+"执行成功，更新了一个挂号单");
		} catch (Exception e) {
			e.printStackTrace();
			json.setMsg("出现错误");
			logger.error(user.getUserTruename()+"更新挂号单失败");
		}
		super.writeJson(json);

	}
	
	//--------get set -----------
	public PatientServiceI getPatientService() {
		return patientService;
	}

	@Autowired
	public void setPatientService(PatientServiceI patientService) {
		this.patientService = patientService;
	}

}
